package com.clstu.map;

import java.util.Comparator;
import java.util.TreeMap;

public class TreeMap_ {
    public static void main(String[] args) {
        //TreeMap treeMap = new TreeMap();//不提供比较器默认无序
        TreeMap treeMap = new TreeMap(new Comparator() {//有comparator安装提供的compare()排序
            @Override
            public int compare(Object o1, Object o2) {
                //return ((String)o1).compareTo((String)o2);
                return ((String)o1).length()-((String)o2).length();//按长度判断key是否相等。
            }
        });//不提供比较器默认无序

        treeMap.put("hello","jack");
        treeMap.put("world","嘿嘿");
        treeMap.put("tom","哈哈哈");
        treeMap.put("smith","der");//key不变，value变，
        treeMap.put("yes","jack");
        treeMap.put("tom","哦耶");//改变tom对应的value，不插入新的对象
        System.out.println(treeMap);
    }
}
